<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>ECharts</title>
    <script src="./qwebchannel.js"></script>
    <!-- 引入 echarts.js -->
    <script type="text/javascript" src="http://echarts.baidu.com/gallery/vendors/echarts/echarts-all-3.js"></script>
</head>
<body>
    <!-- 为ECharts准备一个具备大小（宽高）的Dom -->
    <div id="main" style="width: 1300px;height:768px;"></div>
    <script type="text/javascript">

        // 基于准备好的dom，初始化echarts实例
        var myChart = echarts.init(document.getElementById('main'));
        myChart.on('click', function (params) {
                console.log(params.data);
                // alert(JSON.stringify(params.data.index));  
                onShowMsgBox(params.data.index); 
            });
        var subtext;
        // 指定图表的配置项和数据
        option = {
            title : {
                text: '磁盘空间分析',
                subtext: '爱信不信',
                x:'center'
            },
            tooltip : {
                trigger: 'item',
                x : 'left',
                //formatter: "{a} <br/>{b} : {c} ({d}%)"
                formatter:function(params){
                   // alert(JSON.stringify(params.data));
                    var str = "大小："+params.data.value+"字节<br/>包含："+params.data.filecnt+"个文件,"+params.data.dircnt+"个文件夹";
                    return str;
                }
            },
            legend: {
                orient : 'vertical',
                x : 'right',
                data:[]
            },
            toolbox: {
                show : true,
                feature : {
                    mark : {show: true},
                    dataView : {show: true, readOnly: false},
                    magicType : {
                        show: true, 
                        type: ['pie', 'funnel'],
                        option: {
                            funnel: {
                                x: '25%',
                                width: '50%',
                                funnelAlign: 'left',
                                max: 1548
                            }
                        }
                    },
                    restore : {show: true},
                    saveAsImage : {show: true}
                }
            },
            calculable : true,
            series : [
                 {
                    name:'访问来源',
                    type:'pie',
                    center : ['50%', '50%'],
                    radius : 80,
                    itemStyle : {
                        normal : {
                            label : {
                                position : 'inner',
                                formatter : function (params) { 
                                   // alert(JSON.stringify(params));                        
                                    //return (params.percent - 0).toFixed(0) + '%'
                                    return params.data.size;
                                }
                            },
                            labelLine : {
                                show : false
                            }
                        },
                        emphasis : {
                            label : {
                                show : true,
                                formatter : "{b}\n{d}%"
                            }
                        }
                        
                    },
                    data:[
                        {value:335, name:'直达'},
                        {value:679, name:'营销广告'},
                        {value:1548, name:'搜索引擎'}
                    ]
                },
                {
                    name:'访问来源',
                    type:'pie',
                    center : ['50%', '50%'],
                    radius : [110, 140],
                    data:[
                        {value:335, name:'直达'},
                        {value:310, name:'邮件营销'},
                        {value:234, name:'联盟广告'},
                        {value:135, name:'视频广告'},
                        {
                            value:1048,
                            name:'百度'
                        },
                        {value:251, name:'谷歌'},
                        {
                            value:102,
                            name:'必应',
                            itemStyle : {
                                normal : {
                                    label : {
                                        show : false
                                    },
                                    labelLine : {
                                        show : false
                                    }
                                },
                                emphasis : {
                                    label : {
                                        show : true
                                    },
                                    labelLine : {
                                        show : true,
                                        length : 50
                                    }
                                }
                            }
                        },
                        {value:147, name:'其他'}
                    ]
                }
            ]
        };

        // 使用刚指定的配置项和数据显示图表。
        myChart.setOption(option);

    new QWebChannel(qt.webChannelTransport, function(channel) {
        window.bridge = channel.objects.bridge;
        
        //Web connect the Qt signal, then Qt can call "output" function
        bridge.sendstring2js.connect(function(str) {  
           // alert("Received string from Qt: " + str);  
           subtext = str;
        }); 
        bridge.sendjsonobj2js.connect(function(obj) {  

            data = obj.data;

            var names = new Array();            
            for(var i=0;i<data.length;i++){
                 names.push(data[i].name);
                // alert("Received string from Qt: " + data[i].name);  
            }

            subtext="分析路径："+obj.path+" 大小："+obj.size_s;

            myChart.setOption({
                title : {
                    subtext: subtext,
                    },
                    // legend: {
                    //     data:names
                    // },
                    series: [
                        {
                            // 根据名字对应到相应的系列
                            data:[
                                {
                                    name: obj.path,
                                    value: obj.size,
                                    size: obj.size_s,
                                    index:-1
                                }
                            ]
                                
                        },
                        {
                            // 根据名字对应到相应的系列
                            data: data
                        }
                    ]
              }); 
        });
        bridge.sendjsonarray2js.connect(function(data) {  
           var names = new Array();
            
           for(var i=0;i<data.length;i++){
                names.push(data[i].name);
               // alert("Received string from Qt: " + data[i].name);  
              }
              myChart.setOption({
                title : {
                    subtext: subtext,
                    },
                    legend: {
                        data:names
                    },
                    series: [{
                        // 根据名字对应到相应的系列
                        data: data
                    }]
              }); 

           
        });
      })

        function onShowMsgBox(idx) {
            if (bridge) {
                bridge.showMsgBox(idx);
            }
        }

    </script>
</body>
</html>
<!--<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>test</title>
    <script src="./qwebchannel.js"></script>
  </head>
  <body style="background:#282c34;">
    <h3 style="color:#ffffff;">this is web page</h3>
    <input type="button" value="C++ function callback" onclick="onShowMsgBox()">
    <script>
      
      function onShowMsgBox() {
        if (bridge) {
          bridge.showMsgBox()
        }
      }

      function showAlert() {
        alert('this is web alert');
      }
    </script>
  </body>
</html>-->
